﻿using MobileBSC_Data;
using System;
using System.Collections.Generic;
using System.Data.SQLite;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MobileBSC_SQLite
{
    public class SignalAssociationMapWriter
    {
        private const string INSERT_STMT = "INSERT INTO SignalAssociationMap (WeakSignalID, KPIID, Influence) VALUES (@signalId, @kpiId, @influence);";
        private const string UPDATE_STMT = "UPDATE SignalAssociationMap SET WeakSignalID = @signalId, KPIID = @kpiId, Influence = @influence WHERE SignalAssociationMapID = @id;";
        private const string DELETE_STMT = "DELETE FROM SignalAssociationMap WHERE SignalAssociationMapID = @id;";

        public bool Write(SignalAssociationMap data)
        {
            if (data == null)
                return false;

            return DbTool<SignalAssociationMap>.ExecuteCommand(CreateWriteCommand(data));
        }
        public bool Delete(SignalAssociationMap data)
        {
            if (data == null)
                return false;

            return DbTool<SignalAssociationMap>.ExecuteCommand(CreateDeleteCommand(data.SignalAssociationMapID));
        }
        private SQLiteCommand CreateWriteCommand(SignalAssociationMap data)
        {
            var result = new SQLiteCommand();
            result.CommandType = System.Data.CommandType.Text;

            if (data.SignalAssociationMapID > 0)
            {
                result.CommandText = UPDATE_STMT;
                result.Parameters.Add(new SQLiteParameter("@id", data.SignalAssociationMapID));
            }
            else
                result.CommandText = INSERT_STMT;

            result.Parameters.Add(new SQLiteParameter("@signalId", data.WeakSignal.ID));
            result.Parameters.Add(new SQLiteParameter("@kpiId", data.KPI.ID));
            result.Parameters.Add(new SQLiteParameter("@influence", data.Influence));

            return result;
        }

        private SQLiteCommand CreateDeleteCommand(long id)
        {
            var result = new SQLiteCommand();
            result.CommandType = System.Data.CommandType.Text;
            result.CommandText = DELETE_STMT;
            result.Parameters.Add(new SQLiteParameter("@id", id));

            return result;
        }
    }
}
